<?php
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/Entite.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/database/Database.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/persistence/photo/PhotoDAO.php';
 
class AlbumPhotoDAO {
		
	public static function getListeAlbumPhoto() {
		$result = Database::getInstance() ->executeQueryAndClose("SELECT id, nom,commentaire, id_photo_couverture, DATE_FORMAT(date, '%d/%m/%Y') as dateEuro FROM albumphoto ORDER BY date desc");
		$albumPhotoArray = Array();
		foreach ($result as $row) {
		//foreach ($result as $row) {
			$albumPhotoEntite = AlbumPhotoDAO::getAlbumPhotoFromDatabaseRow($row);
			AlbumPhotoDAO::loadCouverture($albumPhotoEntite);
			$albumPhotoArray[] = $albumPhotoEntite;
		}
		return $albumPhotoArray;
	}
	
	public static function getAlbumPhotoFromDatabaseRow($row) {
		$albumPhotoEntite = new AlbumPhotoEntite();
		$albumPhotoEntite->id = $row['id'];
		$albumPhotoEntite->nom = $row['nom'];
		$albumPhotoEntite->date = $row['dateEuro'];
		$albumPhotoEntite->commentaire = $row['commentaire'];
		
		return $albumPhotoEntite;
	}
	
	public static function loadCouverture($albumPhotoEntite) {
		
		$result = Database::getInstance() ->executeQueryAndClose("SELECT id_photo_couverture FROM albumphoto WHERE id=".$albumPhotoEntite->id);
		foreach ($result as $row) {
		//foreach ($result as $row) {
			$albumPhotoEntite->couverture = PhotoDAO::getPhotoByID($row['id_photo_couverture']);
		}
		return $albumPhotoEntite;
	}
	
	public static function getAlbumPhotoByID($id) {
		
		$result = Database::getInstance()->executeQueryAndClose("SELECT id, nom,commentaire, DATE_FORMAT(date, '%d/%m/%Y') as dateEuro, id_photo_couverture FROM albumphoto WHERE id =$id");
		foreach ($result as $row) {
		//foreach ($result as $row) {
			return AlbumPhotoDAO::getAlbumPhotoFromDatabaseRow($row);
		}
		return null;
	}
	
	public static function save(AlbumPhotoEntite $albumPhotoEntite) {
		if($albumPhotoEntite->id == Entite::getUnpersistedId()) {
			return AlbumPhotoDAO::insert($albumPhotoEntite);
		} else {
			return AlbumPhotoDAO::update($albumPhotoEntite);
		}
	}
	
	public static function delete(AlbumPhotoEntite $albumPhotoEntite) {
		
		$result = Database::getInstance()->executeUpdateQueryAndClose("DELETE FROM albumphoto WHERE id = $albumPhotoEntite->id") ;
		$albumPhotoEntite->id = Entite::getUnpersistedId();
		return $result;
	}
	
	public static function insert(AlbumPhotoEntite $albumPhotoEntite) {
		
		$nom = mysql_escape_string($albumPhotoEntite->nom);
		$commentaire = mysql_escape_string($albumPhotoEntite->commentaire);
		$date = $albumPhotoEntite->date;
		$idPhotoCouverture = Entite::getLienNonRenseigneId();
		if ($albumPhotoEntite->couverture != null) {
			$idPhotoCouverture = $albumPhotoEntite->couverture->id;
		}
		//TODO voir comment remplacer
		$albumPhotoEntite->id = Database::getInstance()->executeInsertQuery("INSERT INTO albumphoto (nom,commentaire, id_photo_couverture, date) values ('$nom','$commentaire', $idPhotoCouverture, STR_TO_DATE('$date','%d/%m/%Y'))") ;
		return $albumPhotoEntite;
	}
	
	public static function insertWithId(AlbumPhotoEntite $albumPhotoEntite) {
		
		$nom = mysql_escape_string($albumPhotoEntite->nom);
		$commentaire = mysql_escape_string($albumPhotoEntite->commentaire);
		$date = $albumPhotoEntite->date;
		$idPhotoCouverture = Entite::getLienNonRenseigneId();
		if ($albumPhotoEntite->couverture != null) {
			$idPhotoCouverture = $albumPhotoEntite->couverture->id;
		}
		
		$result = Database::getInstance()->executeUpdateQueryAndClose("INSERT INTO albumphoto (id, nom,commentaire, id_photo_couverture, date) values ($albumPhotoEntite->id,'$nom','$commentaire', $idPhotoCouverture, STR_TO_DATE('$date','%d/%m/%Y'))") ;
		return $result;
	}
	
	public static function update(AlbumPhotoEntite $albumPhotoEntite) {
		
		$nom = mysql_escape_string($albumPhotoEntite->nom);
		$commentaire = mysql_escape_string($albumPhotoEntite->commentaire);
		$date = $albumPhotoEntite->date;
		$idPhotoCouverture = Entite::getLienNonRenseigneId();
		if ($albumPhotoEntite->couverture != null) {
			$idPhotoCouverture = $albumPhotoEntite->couverture->id;
		} 
		
		$result = Database::getInstance()->executeUpdateQueryAndClose("UPDATE albumphoto SET nom = '$nom', commentaire = '$commentaire', id_photo_couverture = $idPhotoCouverture, date=STR_TO_DATE('$date','%d/%m/%Y') WHERE id = $albumPhotoEntite->id");
		return $result;
	}
}
?>